/*******************************************************************************
 * Copyright (c) 2010-2011 Robert Fraser                                       *
 *                                                                             *
 * Permission is hereby granted, free of charge, to any person obtaining a     *
 * copy of this software and associated documentation files (the "Software"),  *
 * to deal in the Software without restriction, including without limitation   *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
 * and/or sell copies of the Software, and to permit persons to whom the       *
 * Software is furnished to do so, subject to the following conditions:        *
 *                                                                             *
 *   - The above copyright notice and this permission notice shall be included *
 *     in all copies or substantial portions of the Software.                  *
 *                                                                             *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
 * DEALINGS IN THE SOFTWARE.                                                   *
 *******************************************************************************/

using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace zeal.ui
{
    internal static class AssemblyResources
    {
        // For whatever reason a cursor can't be loaded as a traditional resource, but must instead be
        // loaded from an embedded resource stream. Great job, WPF team; always best to keep your
        // limitations totally arbitrary!
        public static readonly Cursor CURSOR_HAND_OPEN = new Cursor(getEmbeddedResource("cursors.handopen.cur"));
        public static readonly Cursor CURSOR_HAND_GRAB = new Cursor(getEmbeddedResource("cursors.handgrab.cur"));

        private const string EMBEDDED_RESOURCE_PREFIX = "zeal.ui.res.";
        private const string PACKED_URI_PREFIX = @"pack://application:,,,/zeal.ui;component/res/";

        public static Stream getEmbeddedResource(string name)
        {
            if(string.IsNullOrEmpty(name))
                return null;
            Stream s = typeof(AssemblyResources).Assembly.GetManifestResourceStream(EMBEDDED_RESOURCE_PREFIX + name);
            if (s == null)
                throw new FileNotFoundException(string.Format(@"The resource file ""{0}"" was not found.", name));
            return s;
        }

        public static BitmapImage getEmbeddedBitmap(string filename)
        {
            BitmapImage result = new BitmapImage();
            result.BeginInit();
            result.StreamSource = getEmbeddedResource(filename);
            result.EndInit();
            return result;
        }

        public static BitmapImage getPackedBitmap(string filename) { return new BitmapImage(getPackedResourceUri(filename)); }
        public static Uri getPackedResourceUri(string filename) { return new Uri(PACKED_URI_PREFIX + filename, UriKind.Absolute); }
        public static T getApplicationResource<T>(string name) { return ((T) Application.Current.FindResource(name)); }
        public static T cloneApplicationResource<T>(string name) where T : Freezable { return (T) getApplicationResource<T>(name).CloneCurrentValue(); }
    }
}